﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace EnterpriseFund.Common
{
    public class CodeImage
    {
        #region 字段
        //宽、高、字体、字体大小
        private int _width, _height, _fontSize, _impurity;
        private Color _color, _bgcolor;
        private string _fontFamily;
        private bool _bold, _italic;
        #endregion
        #region 构造器, 填充字段默认值
        public CodeImage()
        {
            this._width = 60;
            this._height = 20;
            this._fontSize = 12;
            this._fontFamily = "Verdana";
            this._bold = true;
            this._italic = true;
            this._impurity = 3;
            this._color = Color.Blue;
            this._bgcolor = Color.AliceBlue;
        }
        #endregion
        #region 从QueryString读取属性, 重新填充字段值 private void FillImageProperties()
        /// <summary>
        /// 从QueryString读取属性, 重新填充字段值
        /// </summary>
        private void FillImageProperties()
        {
            string str = string.Empty;

            str = Fetch.Get("width");
            if (RegExp.IsNumeric(str)) _width = int.Parse(str);
            if (_width > 300)
            {
                _width = 300;
            }

            str = Fetch.Get("height");
            if (RegExp.IsNumeric(str)) _height = int.Parse(str);
            if (_height > 300)
            {
                _height = 300;
            }

            str = Fetch.Get("fontsize");
            if (RegExp.IsNumeric(str))
            {
                _fontSize = int.Parse(str);
            }

            str = Fetch.Get("fontfamily");
            if (str.Length > 0)
            {
                _fontFamily = str;
            }

            str = Fetch.Get("bold").ToLower();
            if (str.Length > 0)
            {
                _bold = (str == "true");
            }

            str = Fetch.Get("italic").ToLower();
            if (str.Length > 0)
            {
                _italic = (str == "true");
            }

            str = Fetch.Get("impurity");
            if (RegExp.IsNumeric(str))
            {
                _impurity = int.Parse(str);
            }

            if (_impurity > 30)
            {
                _impurity = 30;
            }
        }
        #endregion private void FillImageProperties()

        #region 获取字体修饰设定 	private FontStyle GetFontStyle()
        /// <summary>
        /// 获取字体修饰设定
        /// </summary>
        /// <returns></returns>
        private FontStyle GetFontStyle()
        {
            if (_bold && _italic)
            {
                return (FontStyle.Bold | FontStyle.Italic);
            }
            if (_bold)
            {
                return FontStyle.Bold;
            }
            if (_italic)
            {
                return FontStyle.Italic;
            }
            return FontStyle.Regular;
        }
        #endregion

        #region 得到随机坐标点 private Point[] GetRandomPointGroup()
        /// <summary>
        /// 得到随机坐标点
        /// </summary>
        /// <returns></returns>
        private Point[] GetRandomPointGroup()
        {
            Point[] points = new Point[_impurity * 2];
            for (int i = 0; i < points.Length; i++)
            {
                int seed = new Random().Next(10000000, 99999999);
                int x = new Random((seed / (i + 2)) * (i + 1)).Next(0, _width);
                int y = new Random((seed / (x + 2)) * (x + 1)).Next(0, _height);

                points[i] = new Point(x, y);
            }
            return points;
        }
        #endregion

        #region 生成图片 private Bitmap GetneralCodeImage()
        /// <summary>
        /// 生成图片
        /// </summary>
        /// <returns></returns>
        public byte[] GetneralCodeImage()
        {
            //创建位图
            Bitmap bmp = new Bitmap(_width, _height);
            Graphics g = Graphics.FromImage(bmp);
            try
            {
                g.Clear(_bgcolor);

                //创建画笔对象
                Pen pen = new Pen(Color.Blue, 1);
                pen.DashStyle = DashStyle.DashDotDot;
                Point[] points = this.GetRandomPointGroup();

                //描绘杂质
                for (int i = 0; i < points.Length; i += 2)
                {
                    g.DrawLine(pen, points[i], points[i + 1]);
                    g.DrawEllipse(
                        pen,
                        new Random().Next(5, 10),
                        new Random().Next(5, 10),
                        new Random().Next(5, 50),
                        new Random().Next(5, 50)
                        );
                }

                //创建将被描绘的字符
                string textBeDraw = new Random().Next(1000, 9999).ToString();
                OperateSession.add(GlobalConstant.PARAM_YZM, textBeDraw);

                //描绘字符
                g.DrawString(
                        textBeDraw,
                        new Font(_fontFamily, _fontSize, this.GetFontStyle()),
                        new SolidBrush(_color),
                        0,
                        0
                    );

                g.Dispose();
                //保存图片数据
                MemoryStream stream = new MemoryStream();
                bmp.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return stream.ToArray();

            }
            finally
            {
                g.Dispose();
                bmp.Dispose();
            }

        }

        #endregion
    }
}